package com.zmn.plat.business.impl.base.acceptproject;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.business.interfaces.base.acceptproject.AcceptProjectBService;
import com.zmn.plat.common.dictionary.BaseAcceptProjectTypeEnum;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.common.util.StringToArrayUtil;
import com.zmn.plat.model.entity.base.acceptproject.AcceptProject;
import com.zmn.plat.model.entity.servcategory.ServAssCategoryQuery;
import com.zmn.plat.model.vo.servcategory.ServAssCategoryVO;
import com.zmn.plat.services.interfaces.base.acceptproject.AcceptProjectService;
import com.zmn.plat.services.interfaces.servcategory.ServAssCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述: 验收项目业务接口实现类
 *
 * @author fengxuecai
 * @since 2021/11/26 18:39
 */
@Slf4j
@Service
public class AcceptProjectBServiceImpl implements AcceptProjectBService {

    @Resource
    private ServAssCategoryService servAssCategoryService;

    @Resource
    private AcceptProjectService acceptProjectService;

    @Override
    @Cacheable(cacheNames = "redis10m", key = "'base:listEfficientAcceptProject:servCategId:'+#p0+':categId:'+#p1+':acceptType:'+#p2", unless = "#result == null")
    public List<AcceptProject> listEfficientAcceptProjectByServCategIdAndCategId(Integer servCategId, Integer categId, Integer acceptType) {

        if (NumberUtil.isNullOrZero(servCategId) || NumberUtil.isNullOrZero(categId)) {
            log.error("ServAssCategoryBServiceImpl.listEfficientAcceptProjectByServCategIdAndcategId入参为空,servCategId={},categId={}", servCategId, categId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        if (NumberUtil.isNullOrZero(acceptType)) {
            // 默认工程师验收
            acceptType = BaseAcceptProjectTypeEnum.ENGINEER_ACCEPT.getCode();
        }

        // 查询服务类型-产品分类关联
        List<ServAssCategoryVO> servAssCategories = servAssCategoryService.listByQuery(ServAssCategoryQuery.builder()
                .servCategId(servCategId)
                .categId(categId).build());
        if (CollectionUtil.isNullOrEmpty(servAssCategories)) {
            log.error("ServAssCategoryBServiceImpl.listEfficientAcceptProjectByServCategIdAndcategId未查到服务类型-产品分类关联,servCategId={},categId={}", servCategId, categId);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }
        ServAssCategoryVO servAssCategoryVO = servAssCategories.get(0);
        // 判断验收类型  1.工程师验收 2.用户验收
        String acceptProject = BaseAcceptProjectTypeEnum.USER_ACCEPT.getCode().equals(acceptType) ? servAssCategoryVO.getUserAcceptProject() : servAssCategoryVO.getEngineerAcceptProject();
        if (StringUtil.isBlank(acceptProject)) {
            return Collections.emptyList();
        }
        List<Integer> acceIds = StringToArrayUtil.convertStrsToList(acceptProject);
        if (CollectionUtil.isNullOrEmpty(acceIds)) {
            return Collections.emptyList();
        }
        // 查询验收项目 过滤停用状态服务内容
        List<AcceptProject> acceptProjects = acceptProjectService.listByIds(acceIds);
        return acceptProjects.stream().filter(e -> Objects.equals(GlobalConsts.YES, e.getStatus())).collect(Collectors.toList());
    }
}
